﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="/easyui/demo.css">
	<link rel="stylesheet" type="text/css" href="/easyui/base.css">
	<link rel="stylesheet" type="text/css" href="/lib/Hui-iconfont/1.0.8/iconfont.css" />
	<script type="text/javascript" src="/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
</head>
<body class="easyui-layout">

<div id="main-menu" class="easyui-menu" style="width:150px;">
    <div id="main-batch-operation" data-options="iconCls:'icon-remove'" onclick="deleteByUserId()">删除</div>
    <div class="menu-sep"></div>
    <div id="main-active" data-options="iconCls:'icon-ok'" onclick="activeByUserId()">激活</div>
    <div class="menu-sep"></div>
    <div id="main-activation" data-options="iconCls:'icon-print'">打印一维码</div>
</div>

<div data-options="region:'center',border:false" style="padding:2px;height: auto">
	<table id="admin_table" class="easyui-datagrid" title="" style="width:100%;height:100%;padding-bottom: 2px;"
		   data-options="rownumbers:true, pagination:true,singleSelect:true,url:'/json-manage-json',
           method:'get',
           toolbar:'#tb',
           fitColumns : true,
           onAfterEdit:function(index,row){
           	var userName = row.userName;
           	var displayName = row.displayName;
           	var roleId = row.roleId;
           	var userId = row.userId;
				updateActions(userName,displayName,roleId,userId);
			 },
			 rowStyler : function(index,row){
                if(row.isLock=='Y'){
                     return 'background-color:#B8B8B8;color:#fff;font-weight:bold;';
                }
           },
           showFooter: true" ,
		>
		<thead>
		<tr>
            <th data-options="field:'userName',width:90,align:'center',
					editor:{
						type:'validatebox',
						options:{
							required:true,
							missingMessage:'请输入登录名'
						}
					}">	登录名称</th>
            <th data-options="field:'displayName',width:90,align:'center',
					editor:{
						type:'validatebox',
						options:{
							required:true,
							missingMessage:'请输入显示名称'
						}
					}">显示名称</th>
			<th data-options="field:'roleId',width:90,align:'center',	editor :{
					type:'combobox',
						options:{
							valueField:'value',
							textField:'text',
							url : '/admin-get-role-list',
							editable: false,
							required:true,
							missingMessage:'请选择角色'
						}
				}
			">角色ID</th>
			<th data-options="field:'phone',width:90,align:'center',
				editor:{
						type:'validatebox',
						options:{
							required:true,
							missingMessage:'请输入电话'
						}
					}">电话</th>
			<th data-options="field:'email',width:90,align:'center',
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'请输入电子邮件'
					}
				}">电子邮件</th>
		</tr>
		</thead>
	</table>
</div>

<div id="tb" style="padding:5px;height:auto">
	<div>
		开始时间: <input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" id="from" style="width:120px">
		结束时间: <input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" id="to" style="width:120px" >
		选择角色:
		<input class="easyui-combobox" name="language"id="key"
			   data-options="
				url : '/admin-get-role-list',
				method: 'get',
				valueField:'value',
				textField:'text'
				">
		<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="searchByParameter">搜索</a>
	</div>
</div>
<!-- 格式化时间 -->
<script type="text/javascript">
	function myformatter(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	}
	function myparser(s){
		if (!s) return new Date();
		var ss = (s.split('-'));
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			return new Date(y,m-1,d);
		} else {
			return new Date();
		}
	}


</script>

<!-- 条件查询 -->
<script type="text/javascript">
	$("#searchByParameter").click(function(){
		var from = $("#from").val();
		var to = $("#to").val();
		var key = $("#key").val();
		var handler = '/admin-list-manage?from='+from+"&to="+to+"&key="+key;
		$('#admin_table').datagrid('options').url = handler;
		$('#admin_table').datagrid('reload');
	});
</script>

<script type="text/javascript">
	$(function(){
		var p = $('#admin_table').datagrid().datagrid('getPager');
		p.pagination({
			pageSize: 10, //每页显示的记录条数，默认为10
			pageList: [10, 15, 25], //可以设置每页记录条数的列表
			buttons:[{
				text : '新增',
				iconCls:'icon-add',
				handler:function(){
					var row = $('#admin_table').datagrid('getSelected');
					if (row){
						var index = $('#admin_table').datagrid('getRowIndex', row);
					} else {
						index = 0;
					}
                    $('#admin_table').datagrid('insertRow', {
                        index: index,
                        row:{
                            ISLOCKED:'Y'
                        }
                    });
					$('#admin_table').datagrid('selectRow',index);
					$('#admin_table').datagrid('beginEdit',index);
				}
			},{
				text : '保存',
				iconCls:'icon-save',
				handler:function(){
					var row = $('#admin_table').datagrid('getSelected');
					if (row){
						var index = $('#admin_table').datagrid('getRowIndex', row);
					} else {
						index = 0;
					}
					$('#admin_table').datagrid('endEdit',index);

				}
			},{
				text : '编辑',
				iconCls:'icon-edit',
				handler:function(){
					var row = $('#admin_table').datagrid('getSelected');
					if (row){
						var index = $('#admin_table').datagrid('getRowIndex', row);
						$('#admin_table').datagrid('selectRow',index);
						$('#admin_table').datagrid('beginEdit',index);
					} else {
						$.messager.show({
							title:'提示',
							msg:'请选择需要编辑的用户',
							timeout:3000,
							showType:'slide',
						});
					}
				}
			},{
				text : '删除',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#admin_table').datagrid('getSelected');
					if(!row){
						$.messager.show({
							title:'提示',
							msg:'请选择需要删除的用户',
							timeout:3000,
							showType:'slide',
						});
					}else if(row.isLock=='Y'){
						$.messager.show({
							title:'提示',
							msg:'激活的用户不能删除',
							timeout:3000,
							showType:'slide',
						});
					}else if(row.isLock=='N'){
                        $.messager.confirm('删除信息','确定删除用户:'+row.displayName+"吗?",function(r){
                            if (r){
                                $.ajax({
                                    url : '/admin-delete-user_id',
                                    data : {userId : row.userId},
                                    async : true,
                                    success : function(){
                                        $('#admin_table').datagrid('reload');
                                    }
                                });
                            }
                        });
					}
				}
			}],
			onSelectPage: function (pageNumber, pageSize) {
				var handler = '/admin-list-manage?startCurr='+pageNumber+"&length="+pageSize;
				$('#admin_table').datagrid('options').url = handler;
				$('#admin_table').datagrid('reload');
			}
		});
	})

</script>
<script>
	function updateActions(userName,displayName,roleId,userId){
		var url = '/admin-insert';
		if(userId!=null&&userId!=''){
			url = '/admin-update';
		}
		$.ajax({
			url : url,
			data : {userName : encodeURI(userName),displayName : displayName,roleId:roleId,userId:userId},
			async : true,
			type : 'post',
			success :function () {
				$('#admin_table').datagrid('reload');
			}
		})
	}
</script>
<script>
    $("#admin_table").datagrid({
        onRowContextMenu : function (e, rowIndex, rowData) {
            var row = $('#admin_table').datagrid('getSelected');
			//可以删除
            if(row&&row.isLock=='N'){
                e.preventDefault();
                $('#main-menu').menu('show',{
                    left : e.pageX,
                    top : e.pageY
                });
                $('#main-batch-operation').removeAttr("disabled").css({ "cursor": "pointer", "opacity": "1" });
				$('#main-active').removeAttr("disabled").css({ "cursor": "pointer", "opacity": "1" });
                e.preventDefault();


			//不可删除
            }else if(row&&row.isLock=='Y'){
                e.preventDefault();
                $('#main-menu').menu('show',{
                    left : e.pageX,
                    top : e.pageY
                });
                $('#main-batch-operation').attr('disabled','disabled').css({"cursor": "default", "opacity": "0.4" });
                $('#main-batch-operation').unbind();
                $('#main-active').attr('disabled','disabled').css({"cursor": "default", "opacity": "0.4" });
                $('#main-active').unbind();
                e.preventDefault();
            }else {
                e.preventDefault();
            }
        }
    });

</script>
<script>

	//通过用户ID删除
	function deleteByUserId(){
        var row = $('#admin_table').datagrid('getSelected');
		if(row&&row.isLock=='N'){
            $.messager.confirm('删除信息','确定删除用户:'+row.displayName+"吗?",function(r){
                if (r){
                    $.ajax({
                        url : '/admin-delete-user_id',
                        data : {userId : row.userId},
                        async : true,
                        success : function(){
                            $('#admin_table').datagrid('reload');
                        }
                    });
                }
            });
		}
	}
	//通过用户ID激活
	function activeByUserId(){
		var row = $('#admin_table').datagrid('getSelected');
		if(row&&row.isLock=='N'){
            $.messager.confirm('激活用户','确定激活用户:'+row.userName+"吗?",function(r){
                if (r){
                    $.ajax({
                        url : '/admin-active-user_id',
                        data : {userId : row.userId},
                        async : true,
                        success : function(){
                            $('#admin_table').datagrid('reload');
                        }
                    });
                }
            });
		}

	}
</script>



</body>
</html>